package com.leji.song.mine;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import com.dgl.sdk.util.DBStore;
import com.dgl.sdk.util.SharePreStore;
import com.leji.song.R;
import com.leji.song.mine.model.DS_Address;
import com.leji.song.util.ConstAns;

import java.util.List;

/**
 * Created by colin.ding on 2014/6/17.
 */

public class AdressAdapter extends BaseAdapter {
    protected ListItemView listItemView;
    protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_Address> addressList;

    private DS_Address haveChoosedAdress;

    public AdressAdapter(){};

    public AdressAdapter(Context context, List<DS_Address> addressList){
        this.context = context;
        this.addressList = addressList;

        listContainer = LayoutInflater.from(context);

    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return addressList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    protected static class ListItemView{
        public TextView name;
        public TextView phoneNum;
        public TextView adress;
        public RadioButton selectBtn;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 自定义视图
        listItemView = new ListItemView();
        // 获取list_item布局文件的视图
        if (convertView == null) {
            convertView = listContainer.inflate(R.layout.address_adapter, null);
            // 获取控件对象
            listItemView.name = (TextView) convertView
                    .findViewById(R.id.name);
            listItemView.phoneNum = (TextView) convertView
                    .findViewById(R.id.phoneNum);
            listItemView.adress = (TextView) convertView
                    .findViewById(R.id.adress);
            listItemView.selectBtn = (RadioButton) convertView
                    .findViewById(R.id.selectBtn);

            // 设置控件集到convertView
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }

        convertView.setOnClickListener(new MyClickListener(position));

        listItemView.name.setText(addressList.get(position).getName());
        listItemView.phoneNum.setText(addressList.get(position).getPhoneNum());
        listItemView.adress.setText(addressList.get(position).getAdress());

        if(SharePreStore.readString(ConstAns.CHOOSED_ADDRESS_ID).equals(addressList.get(position).getAddressID())){
            listItemView.selectBtn.setChecked(true);

            haveChoosedAdress = addressList.get(position);

        }else{
            listItemView.selectBtn.setChecked(false);
        }

        convertView.setOnClickListener(new MyClickListener(position));

        return convertView;
    }

    class MyClickListener implements View.OnClickListener {
        private int position;

        public MyClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {

            addressList.get(position).setChoosed(true);

            if(haveChoosedAdress != null){
                haveChoosedAdress.setChoosed(false);
            }

            notifyDataSetChanged();

            SharePreStore.save(ConstAns.CHOOSED_ADDRESS_ID, addressList.get(position).getAddressID());

            ((Activity)context).finish();
        }

    }
}
